// Input

@import '../../style/themes/default';
@import '../../style/mixins/index';
@import './param';

@mixin input {
  width: 100%;
  @include flex();
  > label {
    display: inline-block;
    white-space: nowrap;
    position: relative;
    color: $--x-text-300;
    font-weight: 600;
    @include text-align();
  }
  &-row {
    flex: 1;
    display: flex;
    align-items: center;
    .#{$--x-input-prefix}-row-before:not(.x-input-row-before-template),
    .#{$--x-input-prefix}-row-after:not(.x-input-row-after-template) {
      border: $--x-input-border;
      border-radius: $--x-input-border-radius;
      background-color: $--x-background-a100;
      border-width: 0;
    }
    .#{$--x-input-prefix}-row-before-template {
      display: contents;
      input,
      .x-button {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }
    .#{$--x-input-prefix}-row-after-template {
      display: contents;
      input,
      .x-button {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
      }
    }
    .#{$--x-input-prefix}-row-before-template {
      &:hover,
      &:focus {
        z-index: 1;
      }
    }
    .#{$--x-input-prefix}-row-before-template,
    .#{$--x-input-prefix}-row-after-template {
      input:hover,
      input:focus,
      .x-button:hover,
      .x-button:focus {
        z-index: 1;
      }
    }
    > .#{$--x-input-prefix}-input {
      position: relative;
      width: 100%;
      display: inline-flex;
      align-items: center;

      > input {
        flex: 1;
        width: 100%;
        font-size: $--x-input-font-size;
        background-color: $--x-input-background-color;
        background-image: none;
        border: $--x-input-border;
        border-radius: $--x-input-border-radius;
        border-width: 0;
        box-shadow: $--x-input-box-shadow;
        transition: all $--x-animation-duration-slow;
        @include placeholder();
        &[disabled] {
          color: $--x-input-disabled-color;
        }
        &:focus {
          @include focus();
          outline: 0;
        }
      }
      > #{$--x-prefix}-icon {
        color: $--x-text-400;
        transition: all $--x-animation-duration-base;
        z-index: 2;
      }
      &:hover {
        > input {
          @include hover();
        }
      }
      &:focus {
        > input {
          @include focus();
          outline: 0;
        }
      }
    }
  }

  &-value-template {
    .#{$--x-input-prefix}-input {
      > input {
        color: transparent;
      }
    }
    &-value {
      position: absolute;
      display: inline-block;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      width: 100%;
      left: 0;
      &:hover + input {
        @include hover();
      }
    }
  }
  &-error-message {
    position: absolute;
    bottom: 0;
    left: 0;
    font-size: $--x-input-font-size - 0.125rem;
    color: $--x-danger;
    margin-bottom: calc(calc(#{$--x-input-font-size} + 0.45rem) * -1);
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  &.x-invalid,
  &.x-required {
    > label {
      color: $--x-danger;
    }
    .#{$--x-input-prefix}-input {
      > #{$--x-prefix}-icon {
        color: $--x-danger;
      }
      @include border-error();
    }
  }
  &.x-disabled {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > #{$--x-prefix}-icon {
        color: $--x-text-400;
      }
      > input,
      > .#{$--x-input-prefix}-value-template-value {
        color: $--x-text-400;
        cursor: not-allowed;
        border-color: $--x-border-100;
        background-color: $--x-background-a100;
      }
    }
  }
  &.x-clearable {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > .#{$--x-input-prefix}-clear {
        position: absolute;
        color: $--x-text-400;
        font-size: $--x-input-icon-font-size;
        padding: $--x-input-icon-padding;
        right: 0;
        left: inherit;
        opacity: 0;
        cursor: pointer;
        z-index: 2;
        &:hover {
          color: $--x-text-300;
        }
      }
      // > input,
      // > .#{$--x-input-prefix}-value-template-value {
      //   &:hover + .#{$--x-input-prefix}-clear,
      //   &:focus + .#{$--x-input-prefix}-clear {
      //     opacity: 1;
      //   }
      // }
      &:hover {
        > input,
        > .#{$--x-input-prefix}-value-template-value {
          .#{$--x-input-prefix}-clear {
            opacity: 1;
          }
        }
      }
    }
    &.x-clear-show {
      > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
        > .#{$--x-input-prefix}-clear {
          opacity: 1;
        }
        > input {
          padding-right: calc(#{$--x-input-icon-font-size} + #{$--x-input-icon-padding} * 2) !important;
        }
      }
    }
  }
  &-max-length {
    position: absolute;
    font-size: $--x-font-size-small;
    color: $--x-text-400;
    padding: 0 $--x-input-icon-padding;
    right: 0;
  }
  &.x-direction {
    &-row {
      > label {
        padding: 0 0.5rem 0 0;
      }
    }
    &-row-reverse {
      > label {
        padding: 0 0 0 0.5rem;
      }
    }
    &-column {
      align-items: inherit;
    }
    &-column-reverse {
      align-items: inherit;
    }
  }
  &-icon {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > #{$--x-prefix}-icon {
        position: absolute;
        font-size: $--x-input-icon-font-size;
        padding: $--x-input-icon-padding;
        &.#{$--x-prefix}-icon-spin {
          padding: 0;
          margin: $--x-input-icon-padding;
        }
      }
    }
  }
  &-icon-left {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > input,
      > .#{$--x-input-prefix}-value-template-value {
        padding-left: calc($--x-input-icon-font-size + $--x-input-icon-padding * 2);
      }
      > #{$--x-prefix}-icon {
        left: 0;
      }
    }
  }
  &-icon-right {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > input,
      > .#{$--x-input-prefix}-value-template-value {
        padding-right: calc($--x-input-icon-font-size + $--x-input-icon-padding * 2);
      }
      > #{$--x-prefix}-icon {
        right: 0;
      }
      > .#{$--x-input-prefix}-max-length {
        right: inherit;
        left: 0;
      }
    }
  }
  &-bordered {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > input {
        border-width: $--x-border-width;
      }
    }
    .#{$--x-input-prefix}-row {
      .#{$--x-input-prefix}-row-before:not(.x-input-row-before-template) {
        border-width: $--x-border-width;
        border-right-width: 0;
      }
      .#{$--x-input-prefix}-row-after:not(.x-input-row-after-template) {
        border-width: $--x-border-width;
        border-left-width: 0;
      }
    }
  }
  &-before {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > input {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
      }
    }
    &-template {
      .#{$--x-input-prefix}-input {
        > input {
          margin-left: calc(#{$--x-border-width} * -1);
          &:hover,
          &:focus {
            z-index: 1;
          }
        }
      }
    }
  }
  &-after {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > input {
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
      }
    }
    &-template {
      > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
        > input {
          margin-right: calc(#{$--x-border-width} * -1);
          &:hover,
          &:focus {
            z-index: 1;
          }
        }
      }
    }
  }
  @include size('big', $--x-height-big, 0 $--x-padding-big);
  @include size('large', $--x-height-large, 0 $--x-padding-large);
  @include size('medium', $--x-height-medium, 0 $--x-padding-medium);
  @include size('small', $--x-height-small, 0 $--x-padding-small);
  @include size('mini', $--x-height-mini, 0 $--x-padding-mini);
}

@mixin size($size, $height, $padding) {
  &-#{$size} {
    > .#{$--x-input-prefix}-row > .#{$--x-input-prefix}-input {
      > input,
      > .#{$--x-input-prefix}-row-before:not(.x-input-row-before-template),
      > .#{$--x-input-prefix}-row-after:not(.x-input-row-after-template),
      > .#{$--x-input-prefix}-value-template-value {
        height: $height;
        line-height: $height;
        padding: $padding;
      }
      > .#{$--x-input-prefix}-max-length {
        height: $height;
        line-height: $height;
      }
    }

    > .#{$--x-input-prefix}-row {
      > .#{$--x-input-prefix}-row-before:not(.x-input-row-before-template),
      > .#{$--x-input-prefix}-row-after:not(.x-input-row-after-template) {
        height: $height;
        line-height: $height;
        padding: $padding;
      }
    }

    > label {
      height: $height;
      line-height: $height;
    }
  }
}

@mixin input-group {
  display: flex;

  &-compact {
    > * {
      display: inline-block;
      input,
      .x-button {
        border-radius: 0 !important;
      }
      input:hover,
      input:focus,
      .x-button:hover,
      .x-button:focus {
        z-index: 1;
      }
    }
    > *:first-child {
      input,
      .x-button {
        border-top-left-radius: $--x-border-radius !important;
        border-bottom-left-radius: $--x-border-radius !important;
      }
    }
    > *:last-child {
      input,
      .x-button {
        border-top-right-radius: $--x-border-radius !important;
        border-bottom-right-radius: $--x-border-radius !important;
      }
    }
    > *:not(:last-child) {
      margin-right: calc(#{$--x-border-width} * -1);
    }
  }
}
